package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("client")
public class ClientController {

    @Autowired
    private IClientService clientService;

    @RequestMapping("list")
    @ResponseBody
    public List<Client> list() {
        return clientService.selectAll();
    }

    @RequestMapping("view")
    @RequiresPermissions(value = {"client:view","客户界面"},logical = Logical.OR)
    public String query(ClientQueryObject qo) {
        return "client";
    }

    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"client:saveOrUpdate","客户新增/更新"},logical = Logical.OR)
    @ResponseBody
    public JSONResult saveOrUpdate(Client client) {
        JSONResult result = new JSONResult();
        try {
            clientService.saveOrUpdate(client);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("signing")
    @ResponseBody
    public JSONResult signing(Long id) {
        JSONResult result = new JSONResult();
        try {
            clientService.signing(id);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("query")
    @ResponseBody
    public Object client(ClientQueryObject qo) {
        return clientService.query(qo);
    }

    @RequestMapping("exportExl")
    public void exportExl(HttpServletResponse resp, ClientQueryObject qo) throws Exception{
        //设置下载响应头
        resp.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        //1.创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //建立新的sheet对象（创建工作薄）
        HSSFSheet sheet = wb.createSheet("client");

        //把高级查询加入查询员工列表中
        List<Client> clients = (List<Client>) clientService.query(qo).getRows();

        //创建第一行
        Row row = sheet.createRow(0);
        //设置标题
        row.createCell(0).setCellValue("学校地址");
        row.createCell(1).setCellValue("学校电话");
        row.createCell(2).setCellValue("重要程度");
        row.createCell(3).setCellValue("意向程度");
        row.createCell(4).setCellValue("意向学科");
        row.createCell(5).setCellValue("上次跟进时间");
        row.createCell(6).setCellValue("下次跟进时间");
        row.createCell(7).setCellValue("跟进状态");
        row.createCell(8).setCellValue("学校名称");
        row.createCell(9).setCellValue("联系人");
        row.createCell(10).setCellValue("营销人员");
        row.createCell(11).setCellValue("跟进人员");
        for (int i = 0; i < clients.size(); i++) {
            //从第二行开始创建行
            row = sheet.createRow(i + 1);
            Client client = clients.get(i);
            //创建单元格
            row.createCell(0).setCellValue(client.getAddress());
            row.createCell(1).setCellValue(client.getTel());
            row.createCell(2).setCellValue(client.getImportance());
            row.createCell(3).setCellValue(client.getWantedLevel());
            row.createCell(4).setCellValue(client.getSubject());

            //设置输出日期的单元格格式为日期格式操作
            CreationHelper createHelper = wb.getCreationHelper();
            CellStyle cellStyle = wb.createCellStyle();
            //设置日期格式
            cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy/M/dd"));
            Cell cell = row.createCell(5);
            cell.setCellValue(client.getBeforedate());
            cell.setCellStyle(cellStyle);
            cell = row.createCell(6);
            cell.setCellValue(client.getAfterdate());
            //把格式设置进去
            cell.setCellStyle(cellStyle);

            row.createCell(7).setCellValue(client.getFollowstate());
            row.createCell(8).setCellValue(client.getState());
            row.createCell(9).setCellValue(client.getSchool().getName());
            row.createCell(10).setCellValue(client.getSaleman().getUsername());
            row.createCell(11).setCellValue(client.getFollowupman().getUsername());
        }
        //输出到浏览器
        wb.write(resp.getOutputStream());
    }

    @RequestMapping("delete")
    @ResponseBody
    public void delete(Long id) {
        clientService.delete(id);
    }
}
